// formPackage/pages/article/newsList/newsList.js

import { queryNewsList, queryDynList, queryCommentNotRead, queryNewsListByTag } from "../../../service/article_service"
import { approveDynRequest } from "../../../service/onetoone_service"
import {
  queryProductReplyNumbers,
  queryProductReplyList
} from "../../../service/jb_product_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    commentTags: [
      {
        title: "全部",
        num: 0,
        key: '0'
      },
      {
        title: "好评",
        num: 0,
        key: '1'
      },
      {
        title: "中评",
        num: 0,
        key: '2'
      },
      {
        title: "差评",
        num: 0,
        key: '3'
      },

    ],
    commentTypeIndex: 0,
    switchArr: [
      {
        name: "最新动态",
        key: 1,
        type: 99,
      },
      {
        name: "资讯专区",
        key: "",
        type: null,
      },


    ],
    topTypeIndex: 0,
    // RICH_TEXT(1, "富文本"),
    // MP(3, "公众号")
    newsList: [],
    dataList: [

    ],
    scrollRefreshStatus: true,

    page: 0,
    pageSize: 10,
    searchWords: "",
    newsTag: "",
    notReadList: [],
    isShowLoading: false,//加载状态出现
    isBottomShow: false,//是否没有更多数据了
    isShowPublish: true,
    productId: null,
    page: 1,
    limit: 30,
    starArr: [1, 1, 1, 1, 1],
    replyConfig: {
      goodCount: 0,
      inCount: 0,
      poorCount: 0,
      replyChance: "1",
      replyStar: 5,
      sumCount: 0
    },
    pageType: null,//如果有值为no，那就证明是这个零评价过来的
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (options.productId) {
      this.setData({
        productId: options.productId
      })
    }
    if (options.type == 'no') {
      this.setData({
        pageType: 'no'
      })
    }




    this.queryProductReplyNumbers();
    this.queryProductReplyList();

  },




  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  queryProductReplyNumbers() {
    queryProductReplyNumbers(this.data.productId).then(res => {
      wx.hideLoading();
      console.log("JJLog:评价数量是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          replyConfig: res.data
        })
        this.dealStarShow(this.data.replyConfig.replyStar);
      }
    }).catch(err => {
      console.error("JJLog:queryProductReplyNumbers", err);
    })
  },
  queryProductReplyList() {
    wx.showLoading();
    queryProductReplyList(this.data.productId, this.data.page, this.data.limit, this.data.commentTypeIndex).then(res => {
      wx.hideLoading();
      console.log("JJLog:评价列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          dataList: res.data.list || [],
          isBottomShow: true,
          scrollRefreshStatus: false,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryProductReplyList", err);
    })
  },
  commentTypeChange(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      commentTypeIndex: index,
    })
    this.queryProductReplyList();
  },
  dealStarShow(starNum) {
    let arr = [];
    // for (let i = 0; i < starNum; i++) {
    //   arr.push(1);
    // }
    // let len = arr.length;
    // if (len < 5) {
    //   for (let i = len; i < 5; i++) {
    //     arr.push(0);
    //   }
    // }
    let replyChance = this.data.replyConfig.replyChance;
    if (replyChance > 0.8 || replyChance == 0.8) {
      arr = [1, 1, 1, 1, 1]
    } else if (replyChance > 0.6 && replyChance < 0.8) {
      arr = [1, 1, 1, 1, 0]
    } else {
      arr = [1, 1, 1, 0, 0]
    }

    let cArr = this.data.commentTags;
    cArr[0].num = this.data.replyConfig.sumCount;
    cArr[1].num = this.data.replyConfig.goodCount;
    cArr[2].num = this.data.replyConfig.inCount;
    cArr[3].num = this.data.replyConfig.poorCount;
    this.setData({
      starArr: arr,
      commentTags: cArr,
    })
    if (this.data.pageType == 'no') {
      this.setData({
        starArr: [1, 1, 1, 0, 0]
      })
    }


  },
  //顶部切换点击
  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    let name = e.currentTarget.dataset.name;
    this.setData({
      topTypeIndex: index
    })
    if (index != 0) {
      this.setData({
        newsTag: name,
        isBottomShow: false,
        page: 0,
      })
      this.queryNewsListByTag();
    } else {
      this.setData({
        isBottomShow: false,
        page: 0,
      })
      this.queryDynList();
    }
  },











  //scroll滚动到底:
  scrolltolower() {
    return;
    if (this.data.isBottomShow) {
      return;
    }
    if (this.data.topTypeIndex == 0) {
      //房友圈
      let tempPage = this.data.page + 1;
      this.setData({
        page: tempPage,
        isShowLoading: true
      })


    } else {
      //新闻列表
      let tempPage = this.data.page + 1;
      this.setData({
        page: tempPage,
        isShowLoading: true
      })

    }

  },

  //scroll下拉刷新:
  refreshDownData() {
    this.setData({
      isBottomShow: false,
      page: 1,
      scrollRefreshStatus: true,
    })
    this.queryProductReplyList();


  },





})